home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / FDSet.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  4KB  |  134 lines

  1. #ifndef FDSET_H
  2. #define FDSET_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/FDSet.h,v 3.0 90/05/20 00:19:35 kgorlen Rel $*/
  5.  
  6. /* FDSet.h -- Class for manipulating fd_set objects used by select(2)
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.  
  21. Modification History:
  22.  
  23. $Log:    FDSet.h,v $
  24.  * Revision 3.0  90/05/20  00:19:35  kgorlen
  25.  * Release for 1st edition.
  26.  * 
  27. */
  28.  
  29. #include "Object.h"
  30. #include <libc.h>
  31. #include <sys/types.h>
  32.  
  33. #ifdef SYSV
  34. #include <memory.h>
  35.  
  36. int bcmp(const void* p, const void* q, int n)
  37. {
  38.     return memcmp(p,q,n);
  39. }
  40.  
  41. #endif
  42.  
  43. #ifndef    FD_SETSIZE
  44.  
  45. #include "nihclconfig.h"
  46.  
  47. const unsigned FD_SETSIZE = sizeof(fd_set) * 8;
  48.  
  49. inline void FD_SET(int    fd, fd_set* p)    { ((int*)p)[div_bitsize_int(fd)] |= (1 << mod_bitsize_int(fd)); }
  50. inline void FD_CLR(int    fd, fd_set* p)    { ((int*)p)[div_bitsize_int(fd)] &= ~(1 << mod_bitsize_int(fd)); }
  51. inline bool FD_ISSET(int fd, fd_set* p)    { return ((int*)p)[div_bitsize_int(fd)] & (1 << mod_bitsize_int(fd)); }
  52. inline void FD_ZERO(fd_set* p)        { memset((void*)p, 0, sizeof(fd_set)); }
  53.  
  54. #endif
  55.  
  56. class FDSet;
  57.  
  58. class FDSetRef: public NIHCL {
  59.     fd_set*    p;    // pointer to word containing bit
  60.     int fd;        // file descriptor
  61.     FDSetRef(fd_set& fdset, int fdesc) {
  62.         p = &fdset; fd = fdesc;
  63.     }
  64.     FDSetRef(const FDSetRef& s) {
  65.         p = s.p; fd = s.fd;
  66.     }
  67.     friend FDSet;
  68. public:
  69.     operator bool() const    { return FD_ISSET(fd,p); }
  70.     bool operator=(bool b) {
  71.         if (b) FD_SET(fd,p);
  72.         else FD_CLR(fd,p);
  73.         return b;
  74.     }
  75.     void operator&=(bool b) {
  76.         if (b && FD_ISSET(fd,p)) FD_SET(fd,p);
  77.         else FD_CLR(fd,p);
  78.     }
  79.     void operator|=(bool b) {
  80.         if (b || FD_ISSET(fd,p)) FD_SET(fd,p);
  81.         else FD_CLR(fd,p);
  82.     }
  83.     void operator^=(bool b) {
  84.         if ((b != 0) != (FD_ISSET(fd,p) !=0 )) FD_SET(fd,p);
  85.         else FD_CLR(fd,p);
  86.     }
  87. };
  88.  
  89. class FDSet: public VIRTUAL Object {
  90.     DECLARE_MEMBERS(FDSet);
  91.     static unsigned    dtablesz;   // size of file descriptor table
  92. public:
  93.     static unsigned dtablesize() { return dtablesz; }
  94. private:
  95.     fd_set fs;            // system-defined file descriptor set
  96. protected:        // storer() functions for object I/O
  97.     virtual void storer(OIOofd&) const;
  98.     virtual void storer(OIOout&) const;
  99. public:
  100.     FDSet()                { FD_ZERO(&fs); }
  101.     operator fd_set&()        { return fs; }
  102.     operator const fd_set&() const    { return fs; }
  103.     operator fd_set*()        { return &fs; }
  104.     operator const fd_set*() const    { return &fs; }
  105.     FDSetRef operator[](int    fd)        { return FDSetRef(fs,fd); }
  106.     const FDSetRef operator[](int fd) const    { return FDSetRef(((FDSet*)this)->fs,fd); }
  107.     FDSet operator-(const FDSet&) const;
  108.     FDSet operator&(const FDSet&) const;
  109.     FDSet operator|(const FDSet&) const;
  110.     FDSet operator^(const FDSet&) const;
  111.     bool operator==(const FDSet& a) const    { return !bcmp(&fs,&a.fs,sizeof(fs)); }
  112.     bool operator!=(const FDSet& a)    const    { return !(*this==a); }
  113.     void operator-=(const FDSet&);
  114.     void operator&=(const FDSet&);
  115.     void operator|=(const FDSet&);
  116.     void operator^=(const FDSet&);
  117.     void clr(int fd)    { FD_CLR(fd,&fs); }
  118.     bool includes(unsigned fd) const { return FD_ISSET(fd,&fs); }
  119.     bool isSet(int fd) const    { return FD_ISSET(fd,&fs); }
  120.     void set(int fd)    { FD_SET(fd,&fs); }
  121.     void zero()        { FD_ZERO(&fs); }
  122.     virtual    unsigned capacity() const;  // returns descriptor table size
  123.     virtual    int compare(const Object&) const;
  124.     virtual    void deepenShallowCopy();   // {}
  125.     virtual unsigned hash() const;
  126.     virtual bool isEmpty() const;
  127.     virtual bool isEqual(const Object&) const;
  128.     virtual void printOn(ostream& strm =cout) const;
  129.     virtual    unsigned size()    const;        // returns # of fd bits set
  130.     virtual const Class* species() const;
  131. };
  132.  
  133. #endif
  134.